package com.gdut.wifidemo.presenter;

import com.gdut.wifidemo.CustomApplication;
import com.gdut.wifidemo.R;
import com.gdut.wifidemo.common.RequestCallback;
import com.gdut.wifidemo.contract.WifiContract;
import com.gdut.wifidemo.data.entity.Result;
import com.gdut.wifidemo.model.IWifiModel;
import com.gdut.wifidemo.model.impl.WifiModelImpl;
import com.gdut.wifidemo.strategy.StrategyContext;
import com.gdut.wifidemo.utils.LogUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class WifiPresenter implements WifiContract.Presenter {

    private static final String TAG = "WifiPresenter";
    private WifiContract.View mWifiView;
    private IWifiModel mWifiModel;

    public WifiPresenter(WifiContract.View view) {
        mWifiView = view;
        mWifiModel = new WifiModelImpl();
        mWifiView.setPresenter(this);
    }

    @Override
    public void start() {
        mWifiView.log("Start... [" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()) + "]");
        mWifiView.updateButtonStatus(CustomApplication.getContext().getString(R.string.locating), false);
        mWifiModel.setStrategy(StrategyContext.Strategy.KNN);
        mWifiModel.execute(new RequestCallback<Result>() {
            @Override
            public void onSuccess(Result result) {
                LogUtils.d(TAG, "on Success result = " + result.toString());
                mWifiView.updateButtonStatus(CustomApplication.getContext().getString(R.string.update_location), true);
                mWifiView.updatePositionText(result.getPosition());
                mWifiView.log("Success End [" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()) + "]");
            }

            @Override
            public void onFailure(String message) {
                mWifiView.updateButtonStatus(CustomApplication.getContext().getString(R.string.update_location), true);
                mWifiView.showMessage(message);
                mWifiView.log("Fail End [" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()) + "]");
            }

            @Override
            public void onLog(String log) {
                mWifiView.log(log);
            }
        });
    }

}
